package cn.power.win.win_power.utils;

import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Base64;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


import cn.power.win.win_power.MyApp;

/**
 * @author 何泰樑
 * @ClassName AsShardPreUtils
 * @Description
 * @date 2018-7-27
 * @history 2018-7-27 author: description:
 */
public class AsShardPreUtils {
    private static AsShardPreUtils preferProcesser;
    private SharedPreferences sharedPreferences;
    private String fileName = "aijiayi";

    public AsShardPreUtils() {
    }

    public static AsShardPreUtils getInstant() {
        if (preferProcesser == null) {
            preferProcesser = new AsShardPreUtils();
        }

        return preferProcesser;
    }

    public boolean setStrPreference(String paramName, String paramValue) {
        return this.setStrPreference(fileName, paramName, paramValue);
    }

    public boolean setStrPreference(String fileName, String paramName, String paramValue) {
        this.sharedPreferences = MyApp.context.getSharedPreferences(fileName, 0);
        SharedPreferences.Editor editor = this.sharedPreferences.edit();
        editor.putString(paramName, paramValue);
        return editor.commit();
    }

    public boolean setBoolPreference(String paramName, boolean paramValue) {
        return this.setBoolPreference(fileName, paramName, paramValue);
    }

    public boolean setBoolPreference(String fileName, String paramName, boolean paramValue) {
        this.sharedPreferences =MyApp.context.getSharedPreferences(fileName, 0);
        SharedPreferences.Editor editor = this.sharedPreferences.edit();
        editor.putBoolean(paramName, paramValue);
        return editor.commit();
    }

    public boolean setLongPreference(String paramName, long paramValue) {
        return this.setLongPreference(fileName, paramName, paramValue);
    }

    public boolean setLongPreference(String fileName, String paramName, long paramValue) {
        this.sharedPreferences =MyApp.context.getSharedPreferences(fileName, 0);
        SharedPreferences.Editor editor = this.sharedPreferences.edit();
        editor.putLong(paramName, paramValue);
        return editor.commit();
    }

    public boolean setIntPreference(String paramName, int paramValue) {
        return this.setIntPreference(fileName, paramName, paramValue);
    }

    public boolean setIntPreference(String fileName, String paramName, int paramValue) {
        this.sharedPreferences =MyApp.context.getSharedPreferences(fileName, 0);
        SharedPreferences.Editor editor = this.sharedPreferences.edit();
        editor.putInt(paramName, paramValue);
        return editor.commit();
    }

    public String getStrPreferenceByParamName(String paramName) {
        return this.getStrPreferenceByParamName(fileName, paramName);
    }

    public String getStrPreferenceByParamName(String fileName, String paramName) {
        this.sharedPreferences =MyApp.context.getSharedPreferences(fileName, 0);
        return this.sharedPreferences.getString(paramName, "");
    }

    public boolean getBoolPreferenceByParamName(String paramName, boolean value) {
        return this.getBoolPreferenceByParamName(fileName, paramName, value);
    }

    public boolean getBoolPreferenceByParamName(String fileName, String paramName, boolean value) {
        this.sharedPreferences =MyApp.context.getSharedPreferences(fileName, 0);
        return this.sharedPreferences.getBoolean(paramName, value);
    }

    public boolean getBoolPreferenceByParamName(String paramName) {
        return this.getBoolPreferenceByParamName(fileName, paramName);
    }

    public boolean getBoolPreferenceByParamName(String fileName, String paramName) {
        this.sharedPreferences = MyApp.context.getSharedPreferences(fileName, 0);
        return this.sharedPreferences.getBoolean(paramName, false);
    }

    public Long getLongPreferenceByParamName(String paramName) {
        return this.getLongPreferenceByParamName(fileName, paramName);
    }

    public Long getLongPreferenceByParamName(String fileName, String paramName) {
        this.sharedPreferences = MyApp.context.getSharedPreferences(fileName, 0);
        return this.sharedPreferences.getLong(paramName, -1L);
    }

    public int getIntPreferenceByParamName(String paramName) {
        return this.getIntPreferenceByParamName(fileName, paramName);
    }

    public int getIntPreferenceByParamName(String fileName, String paramName) {
        this.sharedPreferences = MyApp.context.getSharedPreferences(fileName, 0);
        return this.sharedPreferences.getInt(paramName, -1);
    }

    public float getFloatPreferenceByParamName(String paramName) {
        return this.getFloatPreferenceByParamName(fileName, paramName);
    }

    private float getFloatPreferenceByParamName(String fileName, String paramName) {
        this.sharedPreferences =MyApp.context.getSharedPreferences(fileName, 0);
        return this.sharedPreferences.getFloat(paramName, -1.0F);
    }

    public boolean setFloatPreference(String paramName, float paramValue) {
        return this.setFloatPreference(fileName, paramName, paramValue);
    }

    private boolean setFloatPreference(String fileName, String paramName, float paramValue) {
        this.sharedPreferences =MyApp.context.getSharedPreferences(fileName, 0);
        SharedPreferences.Editor editor = this.sharedPreferences.edit();
        editor.putFloat(paramName, paramValue);
        return editor.commit();
    }

    public <T> boolean setListPreference(String paramName, List<T> list) {
        return this.setListPreference(fileName, paramName, list);
    }

    public <T> boolean setListPreference(String fileName, String paramName, List<T> list) {
        this.sharedPreferences =MyApp.context.getSharedPreferences(fileName, 0);
        SharedPreferences.Editor editor = this.sharedPreferences.edit();
        String str = JSON.toJSONString(list);
        editor.putString(paramName, str);
        return editor.commit();
    }

    public <T> List<T> getListPreference(String paramName) {
        return this.getListPreference(fileName, paramName);
    }

    private <T> List<T> getListPreference(String fileName, String paramName) {
        this.sharedPreferences =MyApp.context.getSharedPreferences(fileName, 0);
        String str = this.sharedPreferences.getString(paramName, "");
        List<T> datalist = new ArrayList<T>();
        if (TextUtils.isEmpty(str)) {
            return datalist;
        }
        datalist = JSON.parseObject(str, new TypeReference<List<T>>() {
        });
        return datalist;
    }

    public boolean removeSharedPreferenceByKey(String key) {
        this.sharedPreferences =MyApp.context.getSharedPreferences(fileName, 0);
        SharedPreferences.Editor editor = this.sharedPreferences.edit();
        editor.remove(key);
        return editor.commit();
    }


    /**
     * 存放实体类以及任意类型
     *
     * @param key
     * @param obj
     */
    public void putBean(String key, Object obj) {
        if (obj instanceof Serializable) {// obj必须实现Serializable接口，否则会出问题
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(obj);
                String string64 = new String(Base64.encode(baos.toByteArray(), 0));
                SharedPreferences.Editor editor = this.sharedPreferences.edit();
                editor.putString(key, string64).commit();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            throw new IllegalArgumentException("the obj must implement Serializble");
        }

    }

    public Object getBean(String key) {
        Object obj = null;
        try {
            String base64 = this.sharedPreferences.getString(key, "");
            if (base64.equals("")) {
                return null;
            }
            byte[] base64Bytes = Base64.decode(base64.getBytes(), 1);
            ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            obj = ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
}